var str = require('./a.js');
import $ from 'jquery';
import Vue from 'vue';
import App from'./App.vue';
import 'bootstrap/dist/css/bootstrap.css';
import router from './router.js';
//MinUI
import MintUI from 'mint-ui';
Vue.use(MintUI);
import 'mint-ui/lib/style.css';
//提示框
import { MessageBox } from 'mint-ui';
Vue.prototype.$messagebox = MessageBox;
// 引入mui
import './dist/css/mui.css';
//vue-resource
import VueResource from "vue-resource";
Vue.use(VueResource);

//后端地址路径
Vue.http.options.root='http://www.liulongbin.top:3005';
//vuex
import Vuex from 'vuex';//依赖于vue
Vue.use(Vuex);//挂载到vue上
let shopCar=JSON.parse(localStorage.getItem('shopCar'))|| [];
//console.log(shopCar);
var store = new Vuex.Store({
    state: {
         
         shopCar:shopCar, 
         //在组件中访问数据 只能通过 this.$store.state.*** 来访问
     },
    mutations: {
       addCar(state,shopinfo){
           var flag = false;//默认购物车没有商品
        state.shopCar.some((item)=>{
            if(item.id==shopinfo.id){
                flag=true;
                return flag;//找到就直接结束
            }
        });
        if(flag==true){
            state.shopCar.forEach((item)=>{
               if(item.id==shopinfo.id){
                   item.number+= shopinfo.number;
                } 
            });
        }else{
            state.shopCar.push(shopinfo);
        localStorage.setItem('shopCar',JSON.stringify(state.shopCar));
        }
            console.log(state.shopCar);
            
        },
        removeCar(state,id){
            //console.log(id);
            state.shopCar.forEach((item,index)=>{
                if(item.id==id){
                    state.shopCar.splice(index,1);
                 } 
             });  
             localStorage.setItem('shopCar',JSON.stringify(state.shopCar));
            
        },
        chang(state,id){
            state.shopCar.forEach((item)=>{
                if(item.id==id){
                    item.seleted = !item.seleted; 
                }
            });
             localStorage.setItem('shopCar',JSON.stringify(state.shopCar));
        },
        changeNumberInput(state,obj){
            state.shopCar.forEach((item)=>{
                if(item.id==obj.id){
                    item.number = obj.num; 
                }
            });
            localStorage.setItem('shopCar',JSON.stringify(state.shopCar));
        }
        
     },
     getters:{
        countOption(state){ 
            var count=0 ;
              state.shopCar.forEach((item)=>{
                   count += item.number ;     
                });
            return count;
        },
        getGoodsnumber(state){
            var obj = {};
            state.shopCar.forEach((item)=>{
                obj[item.id]=item.number;
             });
             return obj;
        },
        getGoodsselsted(state){
            var o = {};
            state.shopCar.forEach((item)=>{
                o[item.id]=item.seleted;
             });
             return o;
        },
        getCountAndPrice(state){
            var obj= {
                connt:0,
                price:0.0,
            };
            state.shopCar.forEach((item)=>{
               if(item.seleted){
                   obj.connt+=item.number;
                   obj.price+=item.price*item.number;
               }
             });
             return obj;
        }
            

    }
 });
 
var vm = new Vue({
    el:'#app',
    data:{
        msg:""
    },
    methods:{
    },
    render:c=>c(App),
    router,
    store,
});














